package com.yyk.yykywd.util;

import com.orhanobut.logger.Logger;
import com.yyk.yykywd.config.Constant;

import java.io.File;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;


/**
 * 获取 sign 加密
 */

public class Md5Util2 {
    private static final String TAG = "Md5Util2";
    private static final String hexDigits[] = {"0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    /**
     * 微信支付签名算法sign
     *
     * @param parameters
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String createSign(Map<String, String> parameters) {
//        parameters = new TreeMap<>();
        String SECRET = "YYkG88LGO3";  //后台给的key值
        String T = System.currentTimeMillis() / 1000 + ""; //时间戳
        String APP = "and";  //安卓标识
        parameters.put("t", T);
        parameters.put("app", APP);
        StringBuffer sb = new StringBuffer();
        Iterator<Map.Entry<String, String>> it = parameters.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();
            //签名值不能为空
            if (!entry.getValue().equals("")) {
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
            Logger.e(TAG + "每次追加MD5加密网址" + sb.toString());
        }
        sb.append("key=" + SECRET);
        String sings = sb.toString();
//        String substring = sings.substring(1, sings.length());
        String sign = MD5Encode(sings, "UTF-8").toUpperCase();
        Logger.e(TAG + "MD5加密网址" + sings.toString());
        Logger.e(TAG + "MD5加密网址有等于号" + sings);
        Logger.e(TAG + "MD5签名" + sign);
        return sign;
    }

    /**
     * 上传参数
     *
     * @param param
     * @return
     */
    private static RequestBody convertToRequestBody(String param) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("text/plain"), param);
        return requestBody;
    }

    public static Object createSign2(Map<String, Object> parameters) {
//        parameters = new TreeMap<>();
        parameters.put("t", (Constant.T) + "");
        parameters.put("app", (Constant.APP) + "");

        StringBuffer sb = new StringBuffer();
        Iterator<Map.Entry<String, Object>> it = parameters.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry entry = it.next();

            sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            Logger.e("每次追加MD5加密网址" + sb.toString());
        }
        sb.append("key=" + (convertToRequestBody(Constant.SECRET)));
        String sings = sb.toString();
//        String substring = sings.substring(1, sings.length());
        String sign = MD5Encode(sings, "UTF-8").toUpperCase();
        Logger.e("MD5加密网址" + sings.toString());
        Logger.e("MD5加密网址有等于号" + sings);
        Logger.e("MD5签名" + sign);
        return (sign);
    }

    private static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++)
            resultSb.append(byteToHexString(b[i]));

        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n += 256;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    private static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname))
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes()));
            else
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes(charsetname)));
        } catch (Exception exception) {
        }
        return resultString;
    }
}
